﻿using System;
using System.Web.Mvc;
using System.Reflection;
using Castle.Core;
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace PIM.Web.Infrastruture
{
    public class DependencyInjectHandler
    {

#region Member vars
        private readonly IWindsorContainer _container;
#endregion

#region Constructors
        public DependencyInjectHandler()
        {
            _container = new WindsorContainer(new XmlInterpreter());
        }

        public DependencyInjectHandler(string configPath)
        {
            _container = new WindsorContainer(new XmlInterpreter(configPath));
        }
#endregion

#region Public Methods
        public void RegisterControllersInAssemblly(Assembly assembly)
        {
            _container.Register(AllTypes.Of<IController>()
                                .FromAssembly(assembly)
                                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)));
        }

        public IController GetControllerInstance(Type controllerType)
        {
            return _container.Resolve(controllerType) as IController;
        }

        public T GetInstance<T>()
        {
            return _container.Resolve<T>();
        }
#endregion
    }
}
